//
//  MyOrderCell.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/21.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "MyOrderCell.h"

@interface MyOrderCell ()

@property (nonatomic, strong) NSMutableArray *typeIdArray;

@property (nonatomic, strong) NSMutableArray *orderStatusArray;

@end

@implementation MyOrderCell
+ (instancetype)initTableViewCell:(UITableView *)tableVieW andCellIdentify:(NSString *)cellIdentify{
    MyOrderCell *cell = [tableVieW dequeueReusableCellWithIdentifier:cellIdentify];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:cellIdentify owner:nil options:nil]lastObject];
    }//由于是从xib中创建，所以调用loadNibName的方法
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _callBtn.layer.borderWidth = 0.5;
    
    _callBtn.layer.borderColor = [UIColor redColor].CGColor;
    
    self.typeIdArray = [[NSMutableArray alloc]initWithObjects:@"",@"干洗",@"新房",@"租房",@"苦力",@"丢丢",@"快递",@"家政",@"开锁",@"简历",@"蔬果",@"买车",@"洗车",@"秒杀",@"上户",@"发布出租房信息订单",@"同城快递", nil];
    
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"",nil]];
    //状态1
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"等待商家接单",@"商家已接单",@"等待骑手接单",@"等待骑手取货",@"骑手运输中",@"商家正在洗衣",@"洗衣完成呼叫骑手",@"等待骑手接单",@"等待骑手取货",@"骑手运输中",@"订单已完成",@"订单已完成",@"订单已完成",@"等待骑手接单",@"等待骑手接单",@"等待骑手接单",@"订单取消中",@"订单取消中",@"已送达商家",@"洗衣完成", nil]];
    //状态2
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"咨询中",@"订单已完成", nil]];
    //状态3
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"咨询中",@"订单已完成", nil]];
    //状态4
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"等待商家接单",@"等待商家联系",@"",@"订单已完成",@"订单取消中",@"订单已取消", nil]];
    //状态5
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"已支付",@"未支付", nil]];
    //状态6
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"搜索骑手",@"等待骑手接单",@"等待骑手取件",@"骑手运输中",@"订单已完成",@"重新呼叫骑手",@"订单已取消",@"订单已取消", nil]];
    //状态7
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"等商家接单",@"商家服务中",@"订单已完成",@"订单已完成",@"订单取消中",@"订单已取消", nil]];
    //状态8
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"等商家接单",@"商家服务中",@"订单已完成",@"订单已完成",@"订单已取消", nil]];
    //状态9
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"已支付",@"未支付", nil]];
    //状态10
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"等待商家接单",@"商家备货中",@"等待骑手接单",@"等待骑手取货",@"骑手运输中",@"订单已完成",@"订单已完成",@"等待骑手接单",@"等待骑手接单",@"订单取消中",@"订单已取消", nil]];
    //状态11
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"咨询中",@"订单已完成",@"",@"",@"订单取消中",@"订单已取消", nil]];
    //状态12
     [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"等待商家接单",@"等待商家接单",@"订单已完成",@"订单已完成", nil]];
    //状态13
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"已支付",@"已发货",@"已收货", nil]];
    //状态14
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"等待商家接单",@"订单已完成",@"订单已完成",@"订单已取消", nil]];
    //状态15
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"订单已完成", nil]];
    //状态16
    [self.orderStatusArray addObject:[[NSArray alloc]initWithObjects:@"搜索骑手",@"等待骑手接单",@"等待骑手取件",@"骑手运输中",@"订单已完成",@"重新呼叫骑手",@"订单取消中",@"订单取消中", nil]];

}

- (void)setOrderData:(NSDictionary *)dic{
    [self.labDD setText:[NSString stringWithFormat:@"订单号：%@",[dic objectForKey:@"ordernumber"]]];
    NSInteger typeidIndex = [[dic objectForKey:@"typeid"] integerValue];
    if (typeidIndex < 17) {
        [self.labKL setText:[_typeIdArray objectAtIndex:typeidIndex]];
    }else{
        [self.labKL setText:@"未知类型"];
    }
    
    [self.labTit setText:[dic objectForKey:@"ordername"]];
    [self.labDate setText:[self getyYearsTimeFormat:[dic objectForKey:@"addtime"]]];
    
    NSString *price = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ordermoney"]];
    if (!KStringIsEmpty(price) && [price integerValue]>0) {
        self.labPrice.hidden = NO;
        [self.labPrice setText:[NSString stringWithFormat:@"¥：%@ 元",price]];
    }else{
        self.labPrice.hidden = YES;
    }
#warning 有疑问orderstate == 0
    [self retStatusString:dic];
    //干洗 苦力  快递  家政 开锁 蔬果 洗车 有取消
//    if (typeidIndex == 1 || typeidIndex == 4 || typeidIndex == 6|| typeidIndex == 7 || typeidIndex == 8 || typeidIndex == 10 || typeidIndex == 12) {
        NSInteger statusIndex = [[dic objectForKey:@"orderstate"] integerValue];
        NSInteger iscomment = [[dic objectForKey:@"iscomment"] integerValue];
        switch (typeidIndex) {
            case 1:{
                if (statusIndex == 2 || statusIndex == 3 || statusIndex == 4 || statusIndex == 5 || statusIndex == 14 || statusIndex == 16){
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];
                }if (statusIndex == 8) {
                    //有呼叫按钮和自取按钮、取消变自提
                    self.callBtn.hidden = NO;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"自提" forState:UIControlStateNormal];
                }if (statusIndex == 13 || statusIndex == 12) {
                    [self evaluation:iscomment];
                }if (statusIndex == 6 || statusIndex == 7 || statusIndex == 9 || statusIndex == 10 || statusIndex == 11 || statusIndex == 15 || statusIndex == 17 || statusIndex == 18 || statusIndex == 19 || statusIndex == 20 || statusIndex == 21){
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = YES;
                }
            }
                break;
            case 2:{
                if (statusIndex == 3) {
                    [self evaluation:iscomment];
                }
            }
                break;
            case 3:{
                if (statusIndex == 3) {
                    [self evaluation:iscomment];
                }
            }
                break;
            case 4:{
                if (statusIndex == 2) {
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];
                }if (statusIndex == 5){
                    [self evaluation:iscomment];
                }
            }
                break;
            case 5:{
                self.callBtn.hidden = YES;
                self.btnCancel.hidden = YES;
            }
                break;
            case 6:{
                if (statusIndex == 2 || statusIndex == 3 || statusIndex == 4){
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];
                }if (statusIndex == 6) {
                    [self evaluation:iscomment];
                    
                }if (statusIndex == 7) {
                    self.btnCancel.hidden = YES;
                    self.callBtn.hidden = NO;
                }
            }
                break;
            case 7:{
                if (statusIndex == 2) {
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];
                }if (statusIndex == 4) {
                    [self evaluation:iscomment];
                    
                }if (statusIndex == 5) {
                    //已评价
                    [self haveEvaluation];
                }
            }
                break;
            case 8:{
                if (statusIndex == 2) {
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];
                }if (statusIndex == 4) {
                    //已评价
                    [self haveEvaluation];
                }
                if (statusIndex == 5) {
                    [self evaluation:iscomment];
                }
                
            }
                break;
            case 9:{
                self.callBtn.hidden = YES;
                self.btnCancel.hidden = YES;
            }
            case 10:{
                if (statusIndex == 2 || statusIndex == 3 || statusIndex == 4 || statusIndex == 5) {
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];

                }if (statusIndex == 7) {
                    [self evaluation:iscomment];
                    
                }if (statusIndex == 8) {
                    //已评价
                    [self haveEvaluation];
                }
            }
                break;
            case 11:{
                if (statusIndex == 3) {
                    [self evaluation:iscomment];

                }
            }
                break;
            case 12:{
                if (statusIndex == 2) {
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];
                }if (statusIndex == 4) {
                    //已评价
                    [self haveEvaluation];
                }
                if (statusIndex == 5) {
                    [self evaluation:iscomment];
                    
                }
            }
                break;
            case 13:{
                if (statusIndex == 3) {
                    //收货按钮
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"确认收货" forState:UIControlStateNormal];
                }
            }
                break;
            case 14:{
                if (statusIndex == 2) {
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];
                    
                }if (statusIndex == 3) {
                    [self evaluation:iscomment];
                    
                }
            }
                break;
            case 15:{
                self.callBtn.hidden = YES;
                self.btnCancel.hidden = YES;
                
            }
               break;
            case 16:{
                if (statusIndex == 2 || statusIndex == 3 || statusIndex == 4) {
                    self.callBtn.hidden = YES;
                    self.btnCancel.hidden = NO;
                    [self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];
                }if (statusIndex == 6) {
                    [self evaluation:iscomment];
                }if (statusIndex == 7) {
                    //呼叫按钮
                    self.callBtn.hidden = NO;
                    self.btnCancel.hidden = YES;
                }
            }
                break;
            default:
                break;
        }
        
//    }
    
    //这个iscomment   为2就是已经评价 为3 就是可以评价 为0 的就不要管了 为0的是不能评价的
}

#pragma mark --- 已评价
- (void)haveEvaluation{
    self.callBtn.hidden = YES;
    self.btnCancel.hidden = NO;
    [self.btnCancel setTitle:@"已评价" forState:UIControlStateNormal];
}
#pragma mark --- 评论按钮显示判断
- (void)evaluation:(NSInteger)iscomment{
    self.callBtn.hidden = YES;
    self.btnCancel.hidden = NO;
    if (iscomment == 2) {
        [self.btnCancel setTitle:@"已评价" forState:UIControlStateNormal];
    }if (iscomment == 3) {
        [self.btnCancel setTitle:@"未评价" forState:UIControlStateNormal];
    }
}

- (NSMutableArray *)typeIdArray{
    if (!_typeIdArray) {
        _typeIdArray = [[NSMutableArray alloc]init];
    }
    return _typeIdArray;
}

- (NSString *)getyYearsTimeFormat:(NSString *)dateString{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateString integerValue]];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *timeStr = [df stringFromDate: date];
//    NSString *timeStr = [df stringFromDate:date];
    return timeStr;
}

- (void)retStatusString:(NSDictionary *)dic{
    NSInteger typeidIndex = [[dic objectForKey:@"typeid"] integerValue];
    NSArray *itemArray = [_orderStatusArray objectAtIndex:typeidIndex];
    NSInteger statusIndex = [[dic objectForKey:@"orderstate"] integerValue];
    
    NSString *statusString  = @"";
    if (statusIndex > 0) {
        
        @try {
            
           statusString = [itemArray objectAtIndex:statusIndex - 2];
            
        } @catch (NSException *exception) {
            
            NSLog(@"补货异常---%@",exception);
        } @finally {
            
            [self.labState setText:statusString];
        }
        
    }else{
        [self.labState setText:@"未知状态"];
    }
    
}

- (IBAction)cancelEvent:(UIButton *)sender {
    self.cancelActionButton(sender);
    
}

- (NSMutableArray *)orderStatusArray{
    if (!_orderStatusArray) {
        _orderStatusArray = [[NSMutableArray alloc]init];
    }
    return _orderStatusArray;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
